<template>
    <div>
        <el-table border row-key="id" :data="permissionArr">
            <el-table-column label="名称" prop="name"></el-table-column>
            <el-table-column label="权限值" prop="code"></el-table-column>
            <el-table-column label="修改时间" prop="updateTime"></el-table-column>
            <el-table-column label="操作">
                <template #="{ row, $index }">
                    <el-button type="primary" size="small" :disabled="row.level == 4 ? true : false"
                        @click="addPermission(row)" v-has="`btn.Permission.add`">{{ row.level == 3 ? '添加功能' : '添加菜单'
                        }}</el-button>
                    <el-button type="primary" size="small" :disabled="row.level == 1 ? true : false"
                        @click="updatePermission(row)" v-has="`btn.Permission.update`">编辑</el-button>
                    <el-popconfirm :title="`您确定删除${row.name}这个职位吗？`" width="12rem" icon="Delete"
                        @confirm="deletePermission(row.id)">
                        <template #reference>
                            <el-button type="primary" size="small" :disabled="row.level == 1 ? true : false"
                                v-has="`btn.Permission.remove`">删除</el-button>
                        </template>
                    </el-popconfirm>

                </template>
            </el-table-column>
        </el-table>
        <el-dialog v-model="dialogVisible" :title="menuParams.id ? '更新菜单' : '添加菜单'">
            <el-form>
                <el-form-item label="名称">
                    <el-input placeholder="请输入菜单名称" v-model="menuParams.name"></el-input>
                </el-form-item>
                <el-form-item label="权限">
                    <el-input placeholder="请输入权限值" v-model="menuParams.code"></el-input>
                </el-form-item>
            </el-form>
            <template #footer>
                <el-button type="primary" size="default" @click="save">确认</el-button>
                <el-button type="primary" size="default" @click='cancel'>取消</el-button>
            </template>
        </el-dialog>
    </div>
</template>

<script setup lang="ts">
import { ref, onMounted, reactive } from 'vue'
import { reqAllPermission, reqAddOrUpdatePermission, reqDeleteMenu } from '@/api/acl/menu'
import type { Permission, PermissionList, PermissionResponseData, MenuParams } from '@/api/acl/menu/type'
import { ElMessage } from 'element-plus';

//储存菜单数据的数组
let permissionArr = ref<PermissionList>([])
//控制dialog组件的显示与隐藏
let dialogVisible = ref<boolean>(false)
//储存每项菜单的数据
let menuParams = reactive<MenuParams>({
    code: '',
    level: 0,
    name: '',
    pid: 0
})

//获取菜单数据的方法
const getHasPermission = async () => {
    let result: PermissionResponseData = await reqAllPermission()
    if (result.code == 200) {
        permissionArr.value = result.data
    }
}
//添加菜单或添加功能的按钮的回调
const addPermission = (row: Permission) => {
    //清空数组
    Object.assign(menuParams, {
        id: 0,
        code: '',
        level: 0,
        name: '',
        pid: 0
    })
    dialogVisible.value = true
    //收集新增的菜单的level数值
    menuParams.level = row.level + 1
    //指定要给哪个菜单新增子菜单
    menuParams.pid = (row.id as number)
}
//编辑按钮的回调
const updatePermission = (row: Permission) => {
    dialogVisible.value = true
    Object.assign(menuParams, row)
}
//添加与编辑保存按钮的回调
const save = async () => {
    let result: any = await reqAddOrUpdatePermission(menuParams)
    dialogVisible.value = false
    if (result.code == 200) {
        ElMessage({
            type: 'success',
            message: menuParams.id ? '更新成功' : '添加成功'
        })
        getHasPermission()
    } else {
        ElMessage({
            type: 'error',
            message: menuParams.id ? '更新失败' : '添加失败'
        })
    }
}
//添加与编辑取消按钮的回调
const cancel = () => {
    dialogVisible.value = false
}
//删除按钮的回调
const deletePermission = async (id: number) => {
    let result: any = await reqDeleteMenu(id)
    if (result.code == 200) {
        ElMessage({
            type: 'success',
            message: '删除成功'
        })
        getHasPermission()
    } else {
        ElMessage({
            type: 'error',
            message: '删除失败'
        })
    }
}


onMounted(() => {
    getHasPermission()
})
</script>

<style scoped lang="scss"></style>